package com.pro.back.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pro.back.bean.*;
import com.pro.back.bean.bo.RepairBO;
import com.pro.back.bean.common.ApiRes;
import com.pro.back.bean.po.RepairPO;
import com.pro.back.dao.DeviceMapper;
import com.pro.back.dao.RepairMapper;
import com.pro.back.service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author: jingwenjuan
 * @Description:
 * @Date: 2025-04-19
 */
@Service
public class RepairServiceImpl implements RepairService {


    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private RepairMapper repairMapper;


    /**
     * 分页条件查询所有的报修、检修记录
     */
    @Override
    public ApiRes selectAllRepair(RepairPO po) {
        PageHelper.startPage(po.getPageNum(),po.getPageSize());
        List<RepairBO> list = repairMapper.selectAll(po);
        PageInfo<RepairBO> info = new PageInfo<>(list);
        return new ApiRes(info);
    }


    /**
     * 新增报修记录
     * 1. 查看该设备是否是正常状态，是的话进行新增，否则返回该设备的状态.
     * 2. 新增报修记录：1）device表设置报修状态，2）repair表设置报修状态
     */
    @Override
    @Transactional
    public ApiRes insertRepair(Repair repair) {
        Device device = deviceMapper.selectByPrimaryKey(repair.getDeviceId());
        if (device == null) {
            return new ApiRes(500,null,"该设备不存在，请重试！");
        }
        if (device.getRepairStatus().equals("0")) {
            repair.setApplyDate(new Date());
            repairMapper.insertSelective(repair);

            device.setRepairStatus("1");
            deviceMapper.updateByPrimaryKeySelective(device);

            return new ApiRes("success");
        } else {
            String status = device.getRepairStatus();
            String resStatus = "";
            switch (status) {
                case "0" :
                    resStatus = "正常";
                    break;
                case "1":
                    resStatus = "报修中";
                    break;
                case "2" :
                    resStatus = "检修中";
                    break;
                case "3":
                    resStatus = "报废";
                    break;
            }
            return new ApiRes(500,null,"该设备已经在"+resStatus+",请重新选择");
        }
    }

    /**
     * 审核报修信息
     * 1. 审核通过：1）设备的状态变成检修中，2）检修表的status变成检修中
     * 2. 审核不通过：1）设备的状态变成正常，2）检修表的status变成审核不通过
     */
    @Override
    @Transactional
    public ApiRes updateRepair(Repair repair) {
        String status = repair.getStatus();
        Device device = new Device();
        device.setId(repair.getDeviceId());
        if ("1".equals(status)) {//报修拒绝
            device.setRepairStatus("0");
        } else {//报修通过
//            1）该设备现在没有被借出、2）该设备没有被报废
            Device device1 = deviceMapper.selectByPrimaryKey(repair.getDeviceId());
            if (device1 == null) {
                return new ApiRes(500,null,"该设备不存在，请重新选择设备！");
            }
            if (device1.getBorrowStatus().equals("1")) {
                return new ApiRes(500,null,"该设备已经被借出，请等待用户归还设备后再同意检修！");
            }
            if (device1.getBorrowStatus().equals("3")) {
                return new ApiRes(500,null,"该设备已经报废，不能被检修！");
            }
            repair.setRepairStartDate(new Date());
            device.setRepairStatus("2");
        }

        deviceMapper.updateByPrimaryKeySelective(device);
        repairMapper.updateByPrimaryKeySelective(repair);
        return new ApiRes("success");
    }

    /**
     * 结束检修
     * 1. repair表设置结束检修
     * 2. device设置为正常状态
     */
    @Override
    @Transactional
    public ApiRes exitRepair(Repair repair) {
        Device device = new Device();
        device.setId(repair.getDeviceId());
        device.setRepairStatus("0");
        deviceMapper.updateByPrimaryKeySelective(device);

        repair.setStatus("3");
        repair.setRepairEndDate(new Date());
        repairMapper.updateByPrimaryKeySelective(repair);
        return new ApiRes("success");
    }

    /**
     * 报废设备
     * 1. 判断该设备是否在借出状态,是的话提示不能报废
     * 2. 判断该设备是否在检修中，是的话提示不能报废
     * 2. 把设备的状态置为报废
     */
    @Override
    @Transactional
    public ApiRes delDevice(Long id) {
        DeviceExample ex = new DeviceExample();
        ex.createCriteria().andIdEqualTo(id).andBorrowStatusEqualTo("1");
        List<Device> list = deviceMapper.selectByExample(ex);
        if (list!=null && list.size()>0) {
            return new ApiRes(500,null,"该设备已经被借出，暂时不能报废!");
        }

        DeviceExample example2 = new DeviceExample();
        example2.createCriteria().andRepairStatusEqualTo("2");
        List<Device> devices = deviceMapper.selectByExample(example2);
        if (devices!=null && devices.size()>0) {
            return new ApiRes(500,null,"该设备正在检修中，暂时不能报废！");
        }

        Device device = new Device();
        device.setId(id);
        device.setRepairStatus("3");
        deviceMapper.updateByPrimaryKeySelective(device);
        return new ApiRes("success");
    }

    @Override
    public ApiRes delRepairInfo(Long id) {
        RepairExample ex = new RepairExample();
        ex.createCriteria().andIdEqualTo(id);
        repairMapper.deleteByPrimaryKey(id);
        return new ApiRes("success");
    }

}
